package com.firmboy.io.fixNio;

import java.io.IOException;
import java.util.Random;

/**
 * 测试BIO
 *
 * @author firmboy
 * @create 2018-04-28 上午9:19
 **/
public final class AyTest {
    public static void main(String[] args) throws InterruptedException {
        //启动线程，运行服务器
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    long start = System.currentTimeMillis();
                    ServerBetter.start();
                    long end = System.currentTimeMillis();
                    System.out.println("Server启动耗时:"+(end-start));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        //避免客户端先于服务器启动前执行代码
        Thread.sleep(1000);

        //启动线程，运行客户端
        char operators[] = {'+', '-', '*', '/'};
        Random random = new Random(System.currentTimeMillis());
        new Thread(new Runnable() {
            @SuppressWarnings("static-access")
            @Override
            public void run() {
                while (true) {
                    //随机产生算术表达式
                    String expression = random.nextInt(10) + "" + operators[random.nextInt(4)] + (random.nextInt(10) + 1);
                    Client.send(expression);
                    try {
                        Thread.sleep(random.nextInt(1000));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }


}
